package com.tanqidi.arcgisandroid.base.utils

import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.os.Environment
import android.text.TextUtils
import androidx.compose.ui.unit.TextUnit
import com.esri.arcgisruntime.arcgisservices.LabelDefinition
import com.esri.arcgisruntime.arcgisservices.LabelingPlacement
import com.esri.arcgisruntime.geometry.GeometryType
import com.esri.arcgisruntime.layers.FeatureLayer
import com.esri.arcgisruntime.mapping.labeling.ArcadeLabelExpression
import com.esri.arcgisruntime.mapping.labeling.LabelOverrunStrategy
import com.esri.arcgisruntime.mapping.labeling.SimpleLabelExpression
import com.esri.arcgisruntime.symbology.*
import com.tanqidi.arcgisandroid.R
import com.tanqidi.arcgisandroid.data.layerConfig.LayerConfig
import java.io.File

object SymbolUtils {

    private val textSymbol = TextSymbol().apply {
        size = 11f
        color = Color.BLUE
        outlineColor = Color.WHITE
        haloColor = Color.WHITE
        haloWidth = 2f
    }

    fun symbol(layer: LayerConfig, featureLayer: FeatureLayer){
//        val labelDefinition = LabelDefinition(ArcadeLabelExpression("\$feature.${layer.label} + ' 今年 ' + \$feature.${layer.label}+' 岁'"), textSymbol).apply {
         val labelDefinition = LabelDefinition(ArcadeLabelExpression("\$feature.${layer.label}"), textSymbol).apply {
            when(featureLayer.featureTable.geometryType) {
                GeometryType.POINT -> {
                    //在点的上方中间
                    placement = LabelingPlacement.POINT_ABOVE_CENTER
                    offsetDistance = layer.width.toDouble() * 1.5
                }
                GeometryType.POLYLINE -> {
                    //设置一下偏移量，标注和符号化样式离得太近了，稍微离远一点显示标注
                    offsetDistance = layer.width.toDouble() * 1.5
                    placement = LabelingPlacement.LINE_ABOVE_ALONG
                }
                GeometryType.POLYGON ->{
                    //默认的显示方式就是 AUTOMATIC
                    placement = LabelingPlacement.AUTOMATIC
                }
                else -> {}
            }
            overrunStrategy = LabelOverrunStrategy.ALLOW
        }

        featureLayer.labelDefinitions.add(labelDefinition)
        //开启标注和添加标注表达式
        featureLayer.isLabelsEnabled = true
        when(featureLayer.featureTable.geometryType){
            GeometryType.POINT -> {
                //唯一值渲染器
                val symbol = SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 12F)
                val iconResourceFolder = Environment.getExternalStorageDirectory().absolutePath + File.separator + getString(R.string.app_name) + File.separator + getString(R.string.icon_resource)
                val iconFolder = File(iconResourceFolder)
                if(!TextUtils.isEmpty(layer.icon) && iconFolder.listFiles().isNotEmpty()){
                    val uniqueValueRenderer = UniqueValueRenderer().apply {
                        fieldNames.add(layer.icon)
                    }
                    uniqueValueRenderer.defaultSymbol = symbol

                    iconFolder.listFiles()?.forEachIndexed { index, file ->
                        val iconName = file.name.split(".")[0]
                        val iconResourcePath = iconResourceFolder + File.separator + file.name
                        val bitmap = BitmapFactory.decodeFile(iconResourcePath)
                        val drawable = BitmapDrawable(bitmap)
                        //val iconSymbol = PictureMarkerSymbol("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Recreation/FeatureServer/0/images/e82f744ebb069bb35b234b3fea46deae")
                        val iconSymbol = PictureMarkerSymbol(drawable)
                        iconSymbol.height = 30F
                        iconSymbol.width = 30F
                        // create a unique value for each category
                        val iconUniqueValue = UniqueValueRenderer.UniqueValue("", iconName, iconSymbol, listOf(iconName))
                        uniqueValueRenderer.uniqueValues.add(iconUniqueValue)
                    }
                    featureLayer.renderer = uniqueValueRenderer
                } else {
                    featureLayer.renderer = SimpleRenderer(symbol)
                }

            }
            GeometryType.POLYLINE -> {
                val lineSymbol = SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, layer.color, layer.width.toFloat())
                featureLayer.renderer = SimpleRenderer(lineSymbol)
            }
            GeometryType.POLYGON -> {
                //设置面样式
                val lineSymbol = SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, layer.borderColor, layer.width.toFloat())
                val fillSymbol = SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, layer.color, lineSymbol)
                featureLayer.renderer = SimpleRenderer(fillSymbol)
            }
            else -> {}
        }

    }

}